<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input size="small" style="width:200px;" v-model.trim="filters.policyNo" placeholder="请输入保单号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input size="small" style="width:200px;" v-model.trim="filters.mobile" placeholder="请输入客户手机号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-tooltip class="item" effect="dark" content="发放时间" placement="top">
            <el-date-picker size="small" style="width:200px;" v-model.trim="filters.Time" type="daterange" align="left" placeholder="请选择抽奖时间范围">
            </el-date-picker>
          </el-tooltip>
        </el-form-item>
        <el-form-item>
          <el-select size="small" clearable style="width:200px;" v-model.trim="filters.status" placeholder="请选择抽奖状态">
            <el-option label="无效" value="0"></el-option>
            <el-option label="未抽奖" value="1"></el-option>
            <el-option label="已抽奖" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item style="float:right;">
          <el-button size="small" type="primary" v-show="drawExport" icon="upload2" @click="exportExcels">导出</el-button>
        </el-form-item>
        <el-form-item style="float:right;">
          <el-button size="small" type="primary" v-show="drawQuery" icon="search" v-on:click="getLogs(true)">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="Logs" stripe border :height="winHeight-188" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <!-- <el-table-column prop="id" align="center" label="ID" min-width="50" show-overflow-tooltip>
      </el-table-column> -->
      <el-table-column prop="activityNo" align="center" label="活动单号" width="100" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="activityName" align="center" label="活动名称" min-width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="amount" align="center" label="中奖金额" min-width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="granter" align="center" label="发放人" width="100">
      </el-table-column>
      <!-- <el-table-column prop="openid" align="center" label="客户微信号" min-width="130" show-overflow-tooltip>
      </el-table-column> -->
      <el-table-column prop="userCode" align="center" label="客户编码" width="130">
      </el-table-column>
      <el-table-column prop="telephone" align="center" label="客户手机号" width="130" show-overflow-tooltip>
      </el-table-column>
      <!-- <el-table-column prop="createTime" align="center" label="创建时间" min-width="140" show-overflow-tooltip>
      </el-table-column> -->
      <el-table-column prop="drawTime" align="center" label="抽奖时间" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="type" align="center" label="抽奖类型" width="100" :formatter="formatType">
      </el-table-column>
      <el-table-column prop="status" align="center" label="状态" width="100" :formatter="formatStatus">
      </el-table-column>
      <!-- <el-table-column prop="effictiveDate" align="center" label="生效时间" width="100" show-overflow-tooltip :formatter="formatEffictiveDate">
      </el-table-column>
      <el-table-column prop="invalidDate" align="center" label="失效时间" width="120" show-overflow-tooltip :formatter="formatInvalidDate">
      </el-table-column> -->
      <el-table-column prop="policyNo" align="center" label="保单号" min-width="130" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="drawNo" align="center" label="中奖号" min-width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="remark" align="center" label="备注" width="100" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="操作" fixed='right' width="100">
        <template scope="scope">
          <el-button size="small" @click="handleDetail(scope.$index, scope.row)">详情</el-button>
          <!-- <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->

      <el-pagination style="float:right" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[10, 20, 40, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-col>
    <!--详情界面-->
    <el-dialog title="详情" size="small" v-model="detailFormVisible" :close-on-click-modal="false">
      <el-form :model="detailForm" label-width="120px" ref="detailForm">
        <el-row>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="状态:" prop="productCname">
              <el-input v-model="detailForm.status" :readonly="true" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="类型:" prop="productCname">
              <el-input v-model="detailForm.type" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="用户编码:" prop="productCname">
              <el-input v-model="detailForm.userCode" :readonly="true" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="电话号码:" prop="productCname">
              <el-input v-model="detailForm.telephone" :readonly="true" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="中奖金额:" prop="productCname">
              <el-input v-model="detailForm.amount" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="活动编号:" prop="productCname">
              <el-input v-model="detailForm.activityNo" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="活动名称:" prop="productCname">
              <el-input v-model="detailForm.activityName" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="抽奖时间:" prop="productCname">
              <el-input v-model="detailForm.drawTime" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="生效时间:" prop="productCname">
              <el-input v-model="detailForm.effictiveDate" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="失效时间:" prop="productCname">
              <el-input v-model="detailForm.invalidDate" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="保单号:" prop="productCname">
              <el-input v-model="detailForm.policyNo" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="微信识别号:" prop="productCname">
              <el-input v-model="detailForm.openid" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="中奖号码:" prop="productCname">
              <el-input v-model="detailForm.drawNo" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="20" :md="16" :lg="11">
            <el-form-item label="备注:" prop="productCname">
              <el-input v-model="detailForm.remark" :readonly='true' auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="detailFormVisible = false">取消</el-button>
        <!-- <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button> -->
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'

import commons from '../../common/js/common'
//import NProgress from 'nprogress'
import { mapState, mapActions } from 'vuex'
import { drawLog } from '../../api/api';

export default {
  data() {
    return {
      filters: {
        policyNo: '',
        mobile: '',
        Time: '',
        status: ''
      },
      Logs: [],
      lists: [],
      exportTotal: 0,
      total: 0,
      page: 1,
      pageSize: 10,
      loading: '',
      // fullscreenLoading:false,
      listLoading: false,
      addLoading: false,
      addFormVisible: false,
      detailFormVisible: false,
      drawExport: false,
      drawQuery: false,

      detailForm: {},
      sels: [],//列表选中列
      userid: '',
      addFormRules: {

      },
      IsShow: false
    }
  },
  computed: mapState([
    'winWidth',
    'winHeight'
  ]),
  methods: {
    cellStyle(row, rowIndex, columnIndex) {
    },
    formatEffictiveDate: function (row, column) {
      return commons.getDaytime(row.effictiveDate, 1, false);
    },
    formatInvalidDate: function (row, column) {
      return commons.getDaytime(row.invalidDate, 1, false);
    },
    formatCreatedDate: function (row, column) {
      return commons.getDaytime(row.createTime, 1, false);
    },
    formatType: function (row, column) {
      return row.type == '1' ? '出单奖励' : '推广奖励';
    },
    formatStatus: function (row, column) {
      return row.status == '0' ? '无效' : (row.status == '1' ? '未抽奖' : (row.status == '2' ? '已抽奖' : ''));
    },
    formatTime: function (row, column) {
      return commons.getDaytime(row.createTime, 1, false);
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getLogs(true);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getLogs(true);
    },
    //获取列表
    getLogs(bool) {
      let para = new FormData();
      para.append("pageSize", bool ? this.pageSize : this.exportTotal);
      para.append("pageNumber", bool ? this.page : 1);
      para.append("status", this.filters.status);
      para.append("startDate", commons.changeTimes(this.filters.Time, 'start'));
      para.append("endDate", commons.changeTimes(this.filters.Time, 'end'));
      para.append("policyNo", this.filters.policyNo);
      para.append("mobile", this.filters.mobile);
      this.listLoading = bool;
      // this.fullscreenLoading = bool?false:true;
      if (!bool) {
        this.loading = this.$loading({
          lock: true,
          text: '正在导出',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      }
      drawLog(para).then((res) => {
        this.lists = res.data.result.content;
        this.total = res.data.result.totalElements;
        this.exportTotal = res.data.result.totalElements;
        this.listLoading = false;
        if (!bool) {
          this.loading.close();
          this.export2Excel();
        }
        else {
          this.Logs = res.data.result.content;
        }
      });
    },
    // 详情界面
    handleDetail: function (index, row) {
      this.detailForm = Object.assign({}, row);
      this.detailFormVisible = true;
      this.detailForm.type = row.type == '1' ? '出单奖励' : '推广奖励';
      this.detailForm.status = row.status == '1' ? '未抽奖' : (row.status == '2' ? '已抽奖' : '无效');
      // this.detailForm.effictiveDate = commons.getDaytime(row.effictiveDate, 2, false);
      // this.detailForm.invalidDate = commons.getDaytime(row.invalidDate, 2, false);
    },
    //table导出excels
    exportExcels: function () {
      this.getLogs(false);
    },
    export2Excel: function () {
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../vendor/Export2Excel');
        const tHeader = ['活动单号', '活动名称', '中奖金额', '发放人', '客户微信号', '客户手机号', '客户代码', '状态', '创建时间', '中奖号', '抽奖时间', '生效时间', '失效时间', '保单号', '备注'];
        const filterVal = ['activityNo', 'activityName', 'amount', 'granter', 'openid', 'telephone', 'userCode', 'status', 'createTime', 'drawNo', 'drawTime', 'effictiveDate', 'invalidDate', 'policyNo', 'remark'];
        const list = this.lists;
        for (var i in list) {
          // list[i].effictiveDate = commons.getDaytime(list[i].effictiveDate, 1, false);
          // list[i].invalidDate = commons.getDaytime(list[i].invalidDate, 1, false);
          list[i].status = list[i].status == '0' ? '无效' : (list[i].status == '1' ? '未抽奖' : (list[i].status == '2' ? '已抽奖' : ''));
          list[i].type = list[i].type == '1' ? '出单奖励' : '推广奖励';
        }
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '抽奖明细列表');
      })
    },
    formatJson: function (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    btnShow: function () {
      let thisData = JSON.parse(sessionStorage.getItem('178'));
      let _this = this;
      $.each(thisData, function (i, j) {
        if (thisData[i] == "drawExport") {
          _this.drawExport = true;
        }
        if (thisData[i] == "drawQuery") {
          _this.drawQuery = true;
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    }
  },
  mounted() {
    this.getLogs(true);
    this.btnShow();
  }
}

</script>

<style scoped>

</style>